package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCDemo8 {
    public static void main(String[] args) {
        LoginUserInfo loginUserInfo = InputUtil.getInputObject(new LoginUserInfo(),"欢迎登录");
        try (Connection conn = DBUtil.getConnection();){
            String sql = "SELECT id,username,userpassword,usernickname,userage FROM userinfo WHERE userinfo=? AND password=?";
            PreparedStatement ps = conn.prepareStatement(sql); // 预编译SQL语句
            //为两个？设置对应的值
            ps.setString(1,loginUserInfo.getUserName());
            ps.setString(2,loginUserInfo.getPassword());
            //执行时不再需要传递SQL语句
            ResultSet  rs = ps.executeQuery();
            if (rs.next()){
                String  nickname = rs.getString("nickname");
                System.out.println("登录成功");
            }else{
                System.out.println("登录失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
